import {
  Column,
  Entity,
  PrimaryGeneratedColumn,
  OneToMany,
  ManyToOne,
  ManyToMany,
  JoinTable,
} from 'typeorm';
import { PhotoEntity } from '../photo/photo.entity';
import { CustomEntity } from '../custom/custom.entity';
import { AreaEntity } from '../area/area.entity';
import { AccessEntity } from '../access/access.entity';
import { JobEntity } from '../job/job.entity';
import { DepartmentEntity } from '../department/department.entity';

@Entity({ name: 'users' })
export class UsersEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column('varchar')
  email: string;

  @Column({ length: 20 })
  name: string;

  @Column({ select: false })
  password: string;

  @Column('varchar')
  phone: string;

  @Column({ type: 'date', nullable: true })
  birthDate: string;

  @Column('varchar')
  adress: string;

  @Column('varchar')
  avatar: string;

  @Column('varchar')
  signature: string;

  @Column('varchar')
  title: string;

  @Column('datetime')
  createDate: string;

  @Column({ type: 'varchar', nullable: true })
  sex: string;

  @Column('int')
  isLogin: number;

  @OneToMany(
    () => PhotoEntity,
    photo => photo.user,
  )
  photos: [];

  @OneToMany(
    () => CustomEntity,
    custom => custom.user,
  )
  custom: CustomEntity[];

  // 无中间实体表的配置
  @ManyToMany(
    type => AccessEntity,
    access => access.users,
  )
  @JoinTable({
    name: 'user_access',
    joinColumns: [{ name: 'user_id' }],
    inverseJoinColumns: [{ name: 'access_id' }],
  })
  access: AccessEntity[];

  @ManyToMany(
    type => JobEntity,
    job => job.users,
  )
  @JoinTable({
    name: 'user_job',
    joinColumns: [{ name: 'user_id' }],
    inverseJoinColumns: [{ name: 'job_id' }],
  })
  job: JobEntity[];

  @ManyToOne(
    () => DepartmentEntity,
    Department => Department.users,
  )
  department: DepartmentEntity[];

  @ManyToMany(
    type => DepartmentEntity,
    DepartmentEntity => DepartmentEntity.leaders,
  )
  @JoinTable({
    name: 'leader_chargeDepartment',
    joinColumns: [{ name: 'leader_id' }],
    inverseJoinColumns: [{ name: 'chargeDepartment_id' }],
  })
  chargeDepartment: DepartmentEntity[];

  @ManyToOne(
    () => AreaEntity,
    province => province.provinceUsers,
  )
  province: AreaEntity;

  @ManyToOne(
    () => AreaEntity,
    city => city.cityUsers,
  )
  city: AccessEntity;
}
